import QtQuick
import QtQuick.Controls
import QtQuick.Layouts
//import QtQuick.Controls.Styles

import Shared

/*基础上GamePage.qml，所以有errorMessage,infoMessage。
  有1个title，1个ListView和2个GameButton。
*/
GamePage {
    //property bool v_txt_info_cal: true

    /*QQ会重载吗？
    */
    function init()
    {
        console.log("show Cal.qml")
        row_info.vis= false;
    }

    Text {
        id: txt_title
        anchors.top: parent.top

        text: qsTr("标定")
        color: "green"
        anchors.horizontalCenter: parent.horizontalCenter
        font.pointSize: 48
    }


    //小数位数。
    RowLayout{
        id: row_pt
        anchors.top: txt_title.bottom

        Text {
            text: qsTr("小数位数")
            color: "green"
        }

        ComboBox{
            id: combobox_pt

            model: ["0", "1","2", "3", "4"]

            currentIndex: deviceHandler.cal_pt_idx;
        }

        Button{
            id: btn_pt_rd
            text: qsTr("读取")
            //color: pressed?red:green

            onClicked: {
                console.log("read pt");
                row_info.vis= true;
                deviceHandler.data03h_to_periph(GameSettings.addr_cal_pt_idx, 2); //参数2固定为2。
            }
        }

        Button{
            id: btn_pt_wr
            text: qsTr("写入")
            //color: pressed?red:green
            enabled: deviceHandler.pwd_ok

            onClicked: {
                console.log("write plc addr");
                row_info.vis= true;
                deviceHandler.data10h_to_periph(GameSettings.addr_cal_pt_idx, combobox_pt.currentIndex);
            }
        }
    }


    //最小分度。
    RowLayout{
        id: row_fd
        anchors.top: row_pt.bottom

        Text {
            text: qsTr("最小分度")
            color: "green"
        }

        ComboBox{
            id: combobox_fd

            model: ["01", "02","05", "10", "20", "50"]

            currentIndex: deviceHandler.cal_fd_idx;
        }

        Button{
            id: btn_fd_rd
            text: qsTr("读取")
            //color: pressed?red:green

            onClicked: {
                console.log("read pt");
                row_info.vis= true;
                deviceHandler.data03h_to_periph(GameSettings.addr_cal_fd_idx, 2); //参数2固定为2。
            }
        }

        Button{
            id: btn_fd_wr
            text: qsTr("写入")
            //color: pressed?red:green
            enabled: deviceHandler.pwd_ok

            onClicked: {
                console.log("write fd");
                row_info.vis= true;

                deviceHandler.data10h_to_periph(GameSettings.addr_cal_fd_idx, combobox_fd.currentIndex); //TODO退后实现：最小分度时，写入索引对应的值，不是索引值。
            }
        }
    }


    //最大量程。
    RowLayout{
        id: row_cp
        anchors.top: row_fd.bottom

        Text {
            text: qsTr("最大量程")
            color: "green"
        }

        TextField{
            id: txtfd_cp

            text: deviceHandler.cal_cp                //数值型自动转化为字符串型。
            color: "white"
            inputMask: "000000"
        }

        Button{
            id: btn_cp_rd
            text: qsTr("读取")
            //color: pressed?red:green

            onClicked: {
                console.log("read cp(fsmax)");
                row_info.vis= true;
                deviceHandler.data03h_to_periph(GameSettings.addr_cal_cp, 2); //参数2固定为2。
            }
        }

        Button{
            id: btn_cp_wr
            text: qsTr("写入")
            //color: pressed?red:green
            enabled: deviceHandler.pwd_ok

            onClicked: {
                console.log("write cp(fsmax)");
                row_info.vis= true;
                deviceHandler.data10h_to_periph(GameSettings.addr_cal_cp, Number(txtfd_cp.text));
            }
        }
    }


    //校准密码。1等于000001
    RowLayout{
        id: row_pwd
        anchors.top: row_cp.bottom

        Text {
            text: qsTr("校准密码")
            color: "green"
        }

        TextField{
            id: txtfd_pwd

            text: qsTr("000000")                 //数值型自动转化为字符串型。
            color: "white"
            inputMask: "000000"
        }

        Button{
            id: btn_pwd_rd
            text: qsTr("验证")
            //color: pressed?red:green

            onClicked: {
                console.log("read pwd");
                row_info.vis= true;
                deviceHandler.data10h_to_periph(GameSettings.addr_cal_pwd, Number(txtfd_pwd.text)); //参数2固定为2。
            }
        }
    }


    //提示信息。
    RowLayout{
        property bool vis: true

        id: row_info
        anchors.top: row_pwd.bottom
        anchors.horizontalCenter: parent.horizontalCenter
        //Layout.alignment: Qt.AlignHCenter

        Text{
            text:qsTr(" ")      //占位，以免下面的Text{}隐藏时，造成下面的控件位置变化。
        }

        Text{
            id: txt_info_cal
            anchors.centerIn: parent.Center
            visible: deviceHandler.txt_ok_v

            //text: qsTr("成功")
            text: deviceHandler.txt_info
            color: deviceHandler.txt_color  //"green"
        }
    }


    Timer{
        id: timer_info_cal
        interval: 500
        running: deviceHandler.txt_ok_v && row_info.vis

        onTriggered: {
            console.log("timer tmo")
            row_info.vis= false
            //row_info.txt_info_set.visible= false;   //和绑定属性deviceHandler.txt_ok_v不同步了。
        }
    }


    //零点　0标定
    RowLayout{
        id: row_cal0
        anchors.top: row_info.bottom
        anchors.topMargin: 20

        Text {
            text: qsTr("零点　0标定")
            color: "green"
            //anchors.horizontalCenter: parent.horizontalCenter
        }

        TextField{
            id: load0_val
            text: deviceHandler.cal_t
            color: "white"
            enabled: false      //不可输入
        }

        Button{
            id: btn_cal_0_rd
            text: qsTr("读取")
            //color: pressed?red:green

            onClicked: {
                console.log("CAL 0");
                deviceHandler.data03h_to_periph(GameSettings.addr_cal_0, 0);
            }
        }

        Button{
            id: btn_cal_0_wr
            text: qsTr("标定")
            //color: pressed?red:green
            enabled: deviceHandler.pwd_ok

            onClicked: {
                console.log("CAL 0");
                deviceHandler.data10h_to_periph(GameSettings.addr_cal_0, 0);
            }
        }
    }


    //加载点1标定。
    RowLayout{
        id: row_cal1
        anchors.top: row_cal0.bottom
        anchors.bottomMargin: 80        //QQ无效

        Text {
            text: qsTr("加载点1标定")
            color: "green"
        }

        TextField{
            id: load1_val
            text: deviceHandler.cal_load1
            color: "white"

            inputMask: "#000000"        //'#'是正负号，可有可无。
        }

        Button{
            id: btn_cal_1_rd
            text: qsTr("读取")
            //color: pressed?red:green

            onClicked: {
                console.log("CAL 1");
                deviceHandler.data03h_to_periph(GameSettings.addr_cal_1, 0);
            }
        }

        Button{
            id: btn_cal_1
            text: qsTr("标定1")
            //color: pressed?red:green
            enabled: deviceHandler.pwd_ok

            onClicked: {
                console.log("CAL load1");
                deviceHandler.data10h_to_periph(GameSettings.addr_cal_1, Number(load1_val.text));
            }
        }
    }


    //加载点2标定。
    RowLayout{
        id: row_cal2
        anchors.top: row_cal1.bottom
        anchors.bottomMargin: 80        //QQ无效

        Text {
            text: qsTr("加载点2标定")
            color: "green"
        }

        TextField{
            id: load2_val
            text: deviceHandler.cal_load2
            color: "white"

            inputMask: "#000000"        //'#'是正负号，可有可无。
        }

        Button{
            id: btn_cal_2_rd
            text: qsTr("读取")
            //color: pressed?red:green

            onClicked: {
                console.log("CAL 2");
                deviceHandler.data03h_to_periph(GameSettings.addr_cal_2, 0);
            }
        }

        Button{
            id: btn_cal_2
            text: qsTr("标定2")
            //color: pressed?red:green
            enabled: deviceHandler.pwd_ok

            onClicked: {
                console.log("CAL load2");
                deviceHandler.data10h_to_periph(GameSettings.addr_cal_2, Number(load2_val.text));
            }
        }
    }


    //加载点3标定。
    RowLayout{
        id: row_cal3
        anchors.top: row_cal2.bottom
        anchors.bottomMargin: 80        //QQ无效

        Text {
            text: qsTr("加载点3标定")
            color: "green"
        }

        TextField{
            id: load3_val
            text: deviceHandler.cal_load3
            color: "white"

            inputMask: "#000000"        //'#'是正负号，可有可无。
        }

        Button{
            id: btn_cal_3_rd
            text: qsTr("读取")
            //color: pressed?red:green

            onClicked: {
                console.log("CAL 3");
                deviceHandler.data03h_to_periph(GameSettings.addr_cal_3, 0);
            }
        }

        Button{
            id: btn_cal_3
            text: qsTr("标定3")
            //color: pressed?red:green
            enabled: deviceHandler.pwd_ok

            onClicked: {
                console.log("CAL load1");
                deviceHandler.data10h_to_periph(GameSettings.addr_cal_3, Number(load3_val.text));
            }
        }
    }


    //加载点4标定。
    RowLayout{
        id: row_cal4
        anchors.top: row_cal3.bottom
        anchors.bottomMargin: 80        //QQ无效

        Text {
            text: qsTr("加载点4标定")
            color: "green"
        }

        TextField{
            id: load4_val
            text: deviceHandler.cal_load4
            color: "white"

            inputMask: "#000000"        //'#'是正负号，可有可无。实测输入其他符号（如a,@），变为空格。
        }

        Button{
            id: btn_cal_4_rd
            text: qsTr("读取")
            //color: pressed?red:green

            onClicked: {
                console.log("CAL 4");
                deviceHandler.data03h_to_periph(GameSettings.addr_cal_4, 0);
            }
        }

        Button{
            id: btn_cal_4
            text: qsTr("标定4")
            //color: pressed?red:green
            enabled: deviceHandler.pwd_ok

            onClicked: {
                console.log("CAL load1");
                deviceHandler.data10h_to_periph(GameSettings.addr_cal_4, Number(load4_val.text));
            }
        }
    }


    //帮助信息
    Text {
        //id: txt_title
        anchors.bottom: parent.bottom
        anchors.bottomMargin: 40
        anchors.horizontalCenter: parent.horizontalCenter
        //anchors.centerIn: parent.Center

        text: qsTr("1,2,3,4标定时，须输入重量值\n输入0清除此加载点")
        color: "green"


        font.pointSize:24
    }
}
